﻿
@{
    ViewData["Title"] = "Index";
}

<h1>Index</h1>

<form method="post" enctype="multipart/form-data" asp-controller="Home" asp-action="FileSave">
    <div>
        <div>
            <p>Form表单多个上传文件:</p>
            <input type="file" name="files" multiple />
            <input type="submit" value="上传" />
        </div>
    </div>
</form>

<form id="uploadForm" enctype="multipart/form-data">
    <div>
        <p>AJAX多个上传文件:</p>
        <input type="file" name="fileImage" multiple />   @*表单元素必须加name*@
        <input type="button" id="upload" value="上传" />
    </div>
</form>



@section Scripts
{
    <script>

        $(function () {

            $('#upload').click(function ()
            {
                doUpload();
            });

        });



        function doUpload() {

            //IE10以下的版本不支持,用于移动端没问题
            var formData = new FormData($("#uploadForm")[0]);

            console.log(formData);

            $.ajax({
                url: '@Url.Action("FileSaveAjax")',
                type: 'POST',
                data: formData,
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                success: function (returndata) {
                    alert("上传文件个数:" + returndata.count + ",上传文件总大小:" + returndata.size);
                },

                error: function (returndata) {
                    alert(returndata);
                }
            });
        }
    </script>
}